package com.atguigu.srb.core.service.impl;

import com.atguigu.srb.core.enums.UserBindEnum;
import com.atguigu.srb.core.hfb.FormHelper;
import com.atguigu.srb.core.hfb.HfbConst;
import com.atguigu.srb.core.hfb.RequestHelper;
import com.atguigu.srb.core.mapper.UserInfoMapper;
import com.atguigu.srb.core.pojo.entity.UserBind;
import com.atguigu.srb.core.mapper.UserBindMapper;
import com.atguigu.srb.core.pojo.entity.UserInfo;
import com.atguigu.srb.core.pojo.vo.UserBindVO;
import com.atguigu.srb.core.service.UserBindService;
import com.atguigu.srb.core.service.UserInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 用户绑定表 服务实现类
 * </p>
 *
 * @author fan
 * @since 2021-11-22
 */
@Service
public class UserBindServiceImpl extends ServiceImpl<UserBindMapper, UserBind> implements UserBindService {
    @Resource
    private UserInfoMapper userInfoMapper;

    @Override
    public String bind(UserBindVO userBindVO, String userId) {

        // 保存userBind,注意状态，用户未绑定
        userId = userId;
        QueryWrapper<UserBind> userBindQueryWrapper = new QueryWrapper<>();
        userBindQueryWrapper.eq("user_id", userId);
        UserBind userBind = baseMapper.selectOne(userBindQueryWrapper);
        if (null != userBind) {
            // 更新userBind
            BeanUtils.copyProperties(userBindVO, userBind);
            baseMapper.updateById(userBind);
        } else {
            // 新增userBind
            UserBind userBindInsert = new UserBind();
            userBindInsert.setUserId(userId);
            userBindInsert.setStatus(UserBindEnum.NO_BIND.getStatus());
            BeanUtils.copyProperties(userBindVO, userBindInsert);
            baseMapper.insert(userBindInsert);
        }

        // 生成表单
        Map<String, Object> map = new HashMap<>();
        map.put("agentId", HfbConst.AGENT_ID);
       //map.put("agentUserId", userId);
        map.put("userId", userId);
        map.put("idCard", userBindVO.getIdCard());
        map.put("personalName", userBindVO.getName());
        map.put("bankType", userBindVO.getBankType());
        map.put("bankNo", userBindVO.getBankNo());
        map.put("mobile", userBindVO.getMobile());
        map.put("returnUrl", HfbConst.USERBIND_RETURN_URL);// 同步回调url
        map.put("notifyUrl", HfbConst.USERBIND_NOTIFY_URL);// 异步回调url
        map.put("timestamp", RequestHelper.getTimestamp());
        map.put("sign", RequestHelper.getSign(map));

        String buildForm = FormHelper.buildForm(HfbConst.USERBIND_URL, map);

        return buildForm;
    }

    @Override
    public void chanNotify(Map<String, String[]> parameterMap, Map<String, Object> stringObjectMap) {
        // 修改userBind，userInfo
        //注意agentUserId=userId
        String userId = (String) stringObjectMap.get("agentUserId");
        String bindCode = (String) stringObjectMap.get("bindCode");
        QueryWrapper<UserBind> userBindQueryWrapper = new QueryWrapper<>();
        userBindQueryWrapper.eq("user_id", userId);


        UserBind userBind = baseMapper.selectOne(userBindQueryWrapper);

        userBind.setBindCode(bindCode);
        userBind.setStatus(UserBindEnum.BIND_OK.getStatus());
        baseMapper.updateById(userBind);


        QueryWrapper<UserInfo> userInfoQueryWrapper = new QueryWrapper<>();
        userInfoQueryWrapper.eq("mobile",userId);
        UserInfo userInfo = userInfoMapper.selectOne(userInfoQueryWrapper);

        userInfo.setBindCode(bindCode);
        userInfo.setName(userBind.getName());
        userInfo.setIdCard(userBind.getIdCard());
        userInfo.setBindStatus(UserBindEnum.BIND_OK.getStatus());

        userInfoMapper.updateById(userInfo);


    }

    @Override
    public String getBindCodeByUserId(String userId){
        QueryWrapper<UserBind> userBindQueryWrapper = new QueryWrapper<>();
        userBindQueryWrapper.eq("user_id", userId);
        UserBind userBind = baseMapper.selectOne(userBindQueryWrapper);
        String bindCode = userBind.getBindCode();
        return bindCode;
    }
}
